//输入某二叉树的前序遍历和中序遍历的结果，请构建该二叉树并返回其根节点。 
//
// 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 
//
// 
//
// 示例 1: 
// 
// 
//Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
//Output: [3,9,20,null,null,15,7]
// 
//
// 示例 2: 
//
// 
//Input: preorder = [-1], inorder = [-1]
//Output: [-1]
// 
//
// 
//
// 限制： 
//
// 0 <= 节点个数 <= 5000 
//
// 
//
// 注意：本题与主站 105 题重复：https://leetcode-cn.com/problems/construct-binary-tree-from-
//preorder-and-inorder-traversal/ 
//
// Related Topics 树 数组 哈希表 分治 二叉树 👍 1035 👎 0


package com.leetcode.editor.cn;

import com.leetcode.editor.cn.common.TreeNode;

import java.util.Arrays;
import java.util.List;

public class PJianZhiOffer07ZhongJianErChaShuLcof {
    public static void main(String[] args) {
        Solution solution = new PJianZhiOffer07ZhongJianErChaShuLcof().new Solution();
        int[] preorder = new int[]{3, 9, 20, 15, 7}, inorder = {9, 3, 15, 20, 7};
        TreeNode treeNode = solution.buildTree(preorder, inorder);
        System.out.println(treeNode);

    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            //1.
//            return build(preorder, inorder);
            return build(preorder, inorder, 0, 0, inorder.length - 1);
        }

        //优化，指针递归，不需要额外空间
        private TreeNode build(int[] preorder, int[] inorder, int preStart, int inStart, int inEnd) {
            if (preStart > preorder.length - 1 || inStart > inEnd) {
                return null;
            }
            //前序遍历第一个节点即为当前树的根节点
            TreeNode root = new TreeNode(preorder[preStart]);
            //定位根节点在中序遍历中为位置
            int position = 0;
            //找到当前节点root在中序遍历中的位置，然后再把数组分两半
            for (int i = inStart; i <= inEnd; i++) {
                if (inorder[i] == root.val) {
                    position = i;
                    break;
                }
            }
            root.left = build(preorder, inorder, preStart + 1, inStart, position - 1);
            root.right = build(preorder, inorder, preStart + position - inStart + 1, position + 1, inEnd);            //递归构建左右子树
            return root;
        }

        //切分数组递归
        private TreeNode build(int[] preorder, int[] inorder) {
            if (preorder.length == 0) {
                return null;
            }
            TreeNode root = new TreeNode(preorder[0]);
            int val = root.val;
            int index = 0;
            for (int i : inorder) {
                if (i == val) {
                    break;
                }
                index++;
            }
            int[] leftInorder = Arrays.copyOfRange(inorder, 0, index);
            int[] rightInorder = Arrays.copyOfRange(inorder, index + 1, inorder.length);

            int[] leftPre = Arrays.copyOfRange(preorder, 1, leftInorder.length + 1);
            int[] rightPre = Arrays.copyOfRange(preorder, leftInorder.length + 1, preorder.length);
            root.left = build(leftPre, leftInorder);
            root.right = build(rightPre, rightInorder);

            return root;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}